package com.ssyc.gsk_teacher_appraisal.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.view.MyListView;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.adapter.ReadChooseAnswerAdapter;
import com.ssyc.gsk_teacher_appraisal.adapter.ReadShowAnswerAdapter;
import com.ssyc.gsk_teacher_appraisal.base.BaseLazyFragment;
import com.ssyc.gsk_teacher_appraisal.bean.AnswerInfo;
import com.ssyc.gsk_teacher_appraisal.bean.QuestionDetailsInfo;
import com.ssyc.gsk_teacher_appraisal.bean.ReadLvInfo;
import com.ssyc.gsk_teacher_appraisal.constant.TeacherAppKeys;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用的多题单选的Fragment页面
 * Created by Administrator on 2018/9/4 0004.
 */

public class AppraisalCommonChooseAnswerFragment extends BaseLazyFragment {

    //服务器返回的数据模型
    private QuestionDetailsInfo.SubListBean questionInfo;

    //组装的页面的数据模型
    private AnswerInfo answerInfo;

    //当前题目在大题的位置,需要通过构造函数传递过来
    private int currPos;

    //当前题目的解析
    private String analysis;

    //当前题目的的正确答案
    private String rihgtAnswer;

    //题干
    private TextView tvQuestion;

    //展示提供答题选项的列表
    private MyListView lv;

    //作答,学生成绩时显示
    private LinearLayout llZd;
    private TextView tvOwnAnswer;


    //正确答案,老师查看试卷,老师预览,学生成绩时显示
    private TextView tvRightAnswer;
    private LinearLayout llZqda;

    //全校正确率,只有老师查看试卷的时候显示
    private LinearLayout llQxzql;
    private TextView tvQxzql;

    //展示各个选项的正确率,只有老师查看试卷的时候显示
    private MyListView lvShowAnswer;

    //解析
    private TextView tvParseAnswer;
    private LinearLayout llShowAnswer;
    //答案的数据模型
    private List<ReadLvInfo> oldDatas;

    //展示选择答案列表的适配器
    private ReadChooseAnswerAdapter answerAdapter;


    //页面跳转过来的区分标识,有四种状态 1老师查看试卷,2老师预览试卷,3老师查看学生成绩,4学生进入答题
    private int autherType = -1;

    //选项,最多6个
    private String[] letters = {"A", "B", "C", "D", "E", "F"};


    /**
     * 获取当前页面对象的方法
     *
     * @param subListBean 当前小题的数据
     * @param currPos     当前小题在大题的位置
     * @return
     */
    public static AppraisalCommonChooseAnswerFragment newInstance(QuestionDetailsInfo.SubListBean subListBean, int currPos, String analysis) {
        AppraisalCommonChooseAnswerFragment appraisalCommonChooseAnswerFragment = new AppraisalCommonChooseAnswerFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", subListBean);
        bundle.putInt("curr", currPos);
        bundle.putString("analysis", analysis);
        appraisalCommonChooseAnswerFragment.setArguments(bundle);
        return appraisalCommonChooseAnswerFragment;
    }

    public static AppraisalCommonChooseAnswerFragment newInstance() {
        AppraisalCommonChooseAnswerFragment appraisalCommonChooseAnswerFragment = new AppraisalCommonChooseAnswerFragment();
        Bundle bundle = new Bundle();
        appraisalCommonChooseAnswerFragment.setArguments(bundle);
        return appraisalCommonChooseAnswerFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_teacher_app_common_choose_answer;
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //初始化视图
        initView(view);

        //获取传递的数据
        initIntent();

        //同步界面视图的显示与隐藏状态
        sycViewState();

        //设置展示问题选项的列表数据
        initQuestionLv();

        //设置底部数据(针对老师端进入)
        initBottomData();
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvQuestion = view.findViewById(R.id.tv_question);
        lv = view.findViewById(R.id.lv);
        tvOwnAnswer = view.findViewById(R.id.tv_own_answer);
        llZd = view.findViewById(R.id.ll_zd);
        tvRightAnswer = view.findViewById(R.id.tv_right_answer);
        llZqda = view.findViewById(R.id.ll_zqda);
        tvQxzql = view.findViewById(R.id.tv_qxzql);
        llQxzql = view.findViewById(R.id.ll_qxzql);
        lvShowAnswer = view.findViewById(R.id.lv_show_answer);
        tvParseAnswer = view.findViewById(R.id.tv_parse_answer);
        llShowAnswer = view.findViewById(R.id.ll_show_answer);
    }


    /**
     * 获取传递的数据
     */
    private void initIntent() {
        //从获取跳转进入的类型
        autherType = SPUtil.getInt(getContext(), TeacherAppKeys.AUTHERTYPE);

        //获取当前小题在大题中的位置
        currPos = getArguments().getInt("curr");

        //获取当前小题服务器返回的数据模型
        questionInfo = (QuestionDetailsInfo.SubListBean) getArguments().getSerializable("data");

        //获取当前小题的解析
        analysis = getArguments().getString("analysis");

        //组装页面的数据
        answerInfo = new AnswerInfo();

        //小题的题干
        answerInfo.qustionContent = questionInfo.getTitle();
        if (TextUtils.isEmpty(questionInfo.getTitle())) {
            tvQuestion.setVisibility(View.GONE);
        } else {
            tvQuestion.setVisibility(View.VISIBLE);
            tvQuestion.setText((currPos + 1) + "." + questionInfo.getTitle());
        }

        //用户选择的答案
        String chooseByUser = questionInfo.getStudent_option();
        answerInfo.chooseAnswer = chooseByUser;

        //该小题的正确答案
        String rightAnswerQuestion = questionInfo.getOption();
        answerInfo.rightAnswer = rightAnswerQuestion;

        //小题的全效正确率
        answerInfo.proba = questionInfo.getYes_pro();

        //该小题的解析
        answerInfo.explain = analysis;

        //获取所有选项的集合
        List<String> questionLists = questionInfo.getList();

        //创建所有选项的数据
        List<ReadLvInfo> readLvInfos = new ArrayList<>();
        for (int i = 0; i < questionLists.size(); i++) {

            ReadLvInfo info = new ReadLvInfo();

            //选项的索引ABCDEF等
            info.questionIndex = letters[i];

            //用户选择答案
            info.chooseAnswer = chooseByUser;

            //该小题的正确答案
            info.rightAnswer = rightAnswerQuestion;

            //选项的内容
            info.questionContent = questionLists.get(i);

            //选项的选择率
            //获取选项选择率的对象
            QuestionDetailsInfo.SubListBean.ProBean pro = questionInfo.getPro();
            if (null != pro) {
                if (i == 0) {
                    //选项A
                    if (TextUtils.isEmpty(pro.getA())) {
                        info.probability = "0.0%";
                    } else {
                        info.probability = pro.getA();
                    }
                } else if (i == 1) {
                    //选项B
                    if (TextUtils.isEmpty(pro.getB())) {
                        info.probability = "0.0%";
                    } else {
                        info.probability = pro.getB();
                    }
                } else if (i == 2) {
                    //选项C
                    if (TextUtils.isEmpty(pro.getC())) {
                        info.probability = "0.0%";
                    } else {
                        info.probability = pro.getC();
                    }
                } else if (i == 3) {
                    //选项D
                    if (TextUtils.isEmpty(pro.getD())) {
                        info.probability = "0.0%";
                    } else {
                        info.probability = pro.getD();
                    }
                } else if (i == 4) {
                    //选项E
                    if (TextUtils.isEmpty(pro.getE())) {
                        info.probability = "0.0%";
                    } else {
                        info.probability = pro.getE();
                    }
                } else if (i == 5) {
                    //选项F
                    if (TextUtils.isEmpty(pro.getF())) {
                        info.probability = "0.0%";
                    } else {
                        info.probability = pro.getF();
                    }
                }
            }

            readLvInfos.add(info);
        }
        answerInfo.readLvInfos = readLvInfos;

        oldDatas = answerInfo.readLvInfos;
    }


    /**
     * 根据教师端和学生端不同入口状态
     * 同步界面视图的显示与隐藏方法
     */
    private void sycViewState() {
        if (autherType == 1) {
            //老师查看全校试卷
            //作答不显示
            llZd.setVisibility(View.GONE);

        } else if (autherType == 2) {
            //老师预览试卷
            //作答不显示
            llZd.setVisibility(View.GONE);
            //全校正确率不显示
            llQxzql.setVisibility(View.GONE);
            //选项正确率的列表不显示
            lvShowAnswer.setVisibility(View.GONE);

        } else if (autherType == 3) {
            //老师查看某个学生的试卷或者学生查看自己的试卷
            //全校正确率不显示
            llQxzql.setVisibility(View.GONE);
            //选项正确率的列表不显示
            lvShowAnswer.setVisibility(View.GONE);

        } else if (autherType == 4) {
            //学生进入答题
            llShowAnswer.setVisibility(View.GONE);
        }
    }

    /**
     * 设置答案选项列表ListView的方法
     */
    private void initQuestionLv() {

        if (autherType == 1 || autherType == 2) {
            //老师端查看试卷和老师预览,不需要展示学生选择的答案
            answerAdapter = new ReadChooseAnswerAdapter(1, 0, getContext(), oldDatas, R.layout.teacher_class_lv_read_choose_answer);
        } else if (autherType == 3) {
            //老师端查看学生成绩,需要展示学生选择的答案
            answerAdapter = new ReadChooseAnswerAdapter(1, 1, getContext(), oldDatas, R.layout.teacher_class_lv_read_choose_answer);
        } else if (autherType == 4) {
            //学生端
            answerAdapter = new ReadChooseAnswerAdapter(0, getContext(), oldDatas, R.layout.teacher_class_lv_read_choose_answer);
            //设置条目点击事件
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                    if (oldDatas.get(pos).isSelected) {
                        return;
                    } else {
                        //同步数据状态
                        answerAdapter.upDateStete(pos);
                        //保存选择的答案
                        //通过EventBs将用户作答本小题的相关信息传递给TkMainActivity保存
                        //currPos对应的每个大题的小题的位置, 多题单选
                        int choosePos = currPos;
                        //是否如正确
                        boolean chooseResult = false;
                        if (oldDatas.get(pos).rightAnswer.equals(oldDatas.get(pos).questionContent)) {
                            //用户作答正确
                            chooseResult = true;
                        } else {
                            //用户作答错误
                            chooseResult = false;
                        }
                        BusInfo busInfo = new BusInfo();
                        busInfo.tkMsg = TeacherAppKeys.TKMsg;
                        busInfo.tkChoosePos = choosePos;
                        busInfo.tkChooseAnswer = oldDatas.get(pos).questionIndex;
                        busInfo.tkChooseResult = chooseResult;
                        EventBus.getDefault().post(busInfo);
                    }
                }
            });
        }

        lv.setAdapter(answerAdapter);
    }

    /**
     * 老师端进入的时候设置底部数据的方法
     */
    private void initBottomData() {

        if (llShowAnswer.getVisibility() == View.VISIBLE) {
            //非学生端进入时需要显示底部数据

            if (lvShowAnswer.getVisibility() == View.VISIBLE) {
                //正确率需要显示时才设置选项正确率的列表
                ReadShowAnswerAdapter answerAdapter = new ReadShowAnswerAdapter(getContext(), oldDatas, R.layout.teacher_class_lv_read_show_answer);
                lvShowAnswer.setAdapter(answerAdapter);
            }

            //设置作答
            if (llZd.getVisibility() == View.VISIBLE) {
                //作答需要显示时才设置作答内容
                if (TextUtils.isEmpty(answerInfo.chooseAnswer)) {
                    tvOwnAnswer.setVisibility(View.INVISIBLE);
                } else {
                    tvOwnAnswer.setVisibility(View.VISIBLE);
                    tvOwnAnswer.setText(answerInfo.chooseAnswer);
                    if (answerInfo.chooseAnswer.equals(answerInfo.rightAnswer)) {
                        tvOwnAnswer.setBackgroundResource(R.drawable.teacher_class_tv_right_answer);
                    } else {
                        tvOwnAnswer.setBackgroundResource(R.drawable.teacher_class_tv_own_choose_answer);
                    }
                }
            }

            //正确答案
            tvRightAnswer.setText(answerInfo.rightAnswer);

            //设置解析
            tvParseAnswer.setText(answerInfo.explain);

            //设置全校正确率
            if (llQxzql.getVisibility() == View.VISIBLE) {
                //全校争取率需要显示时才设置
                tvQxzql.setText(answerInfo.proba);
            }

        }
    }

}
